import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/*
 * operationUnit.js - 业务实体 model
 * @date: 2018-10-29
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryOperationUnit as _queryOperationUnit, saveOperationUnit as _saveOperationUnit } from "../services/operationUnitService";
export default {
  namespace: 'operationUnit',
  state: {
    list: {} // 业务实体列表

  },
  effects: {
    // 查询业务实体
    queryOperationUnit: /*#__PURE__*/_regeneratorRuntime.mark(function queryOperationUnit(_ref, _ref2) {
      var payload, call, put, res, list, pagination;
      return _regeneratorRuntime.wrap(function queryOperationUnit$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(_queryOperationUnit, payload);

            case 4:
              res = _context.sent;
              list = getResponse(res);
              pagination = createPagination(list);

              if (!list) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  list: list,
                  pagination: pagination
                }
              });

            case 10:
              return _context.abrupt("return", list);

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, queryOperationUnit);
    }),
    // 新增或更新业务实体
    saveOperationUnit: /*#__PURE__*/_regeneratorRuntime.mark(function saveOperationUnit(_ref3, _ref4) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveOperationUnit$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_saveOperationUnit, payload);

            case 4:
              res = _context2.sent;
              return _context2.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, saveOperationUnit);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};